package org.springbus.habit.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springbus.habit.emus.ResponseCode;
import org.springbus.habit.entity.ActivityRecord;
import org.springbus.habit.entity.ConsumerInfo;
import org.springbus.habit.service.ActivityRecordService;
import org.springbus.habit.web.response.CommonResponse;
import org.springbus.habit.web.response.ConsumerResponse;
import org.springbus.habit.web.response.PageResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 属性标签表 前端控制器
 * </p>
 *
 * @author Mr.Han
 * @since 2024-05-16
 */
@Tag(name = "客户活动记录管理")
@RestController
public class ActivityRecordController {

    @Resource
    private ActivityRecordService activityRecordService;

    @GetMapping("/admin/acl/activity/{page}/{limit}")
    @Operation(summary = "分页查询客户活动记录信息")
    public PageResponse<ActivityRecord> getPageList(@Parameter(description = "当前页码", required = true) @PathVariable Long page,
                                                      @Parameter(description = "每页条数", required = true) @PathVariable Long limit,
                                                      @Parameter(description = "活动名称") String activityName) {
        Page<ActivityRecord> infoPage = activityRecordService.page(new Page<>(page, limit),
                new QueryWrapper<ActivityRecord>().likeRight(StringUtils.isNoneBlank(activityName), "name", activityName)
                        .eq("deleted", false));
        if (Objects.isNull(infoPage) || infoPage.getRecords().isEmpty()) {
            return new PageResponse<>(Lists.newArrayList(), 0L);
        }
        return new PageResponse<>(infoPage.getRecords(), infoPage.getTotal());
    }

    @PutMapping("/admin/acl/activity/update")
    @Operation(summary = "修改客户活动记录")
    public CommonResponse<Boolean> updateUser(@Parameter(description = "活动记录", required = true)@RequestBody ActivityRecord info) {
        if (Objects.isNull(info) || Objects.isNull(info.getId())){
            return new CommonResponse<>(ResponseCode.SYSTEM_ERROR);
        };
        info.setGmtModified(new Date());
        activityRecordService.updateById(info);
        return new CommonResponse<>(true);
    }

    @PostMapping("/admin/acl/activity/save")
    @Operation(summary = "新增客户活动记录")
    public CommonResponse<Boolean> saveUser(@Parameter(description = "活动记录", required = true)@RequestBody ActivityRecord info) {
        if (Objects.isNull(info)){
            return new CommonResponse<>(ResponseCode.SYSTEM_ERROR);
        }
        info.setGmtCreate(new Date());
        activityRecordService.save(info);
        return new CommonResponse<>(true);
    }

    @DeleteMapping("/admin/acl/activity/remove/{id}")
    @Operation(summary = "删除客户活动记录")
    public CommonResponse<Boolean> removeUser(@Parameter(description = "活动名称ID", required = true)@PathVariable String id) {
        activityRecordService.update(new ActivityRecord().setDeleted(true).setGmtModified(new Date()),new QueryWrapper<ActivityRecord>()
                .eq("id",Long.valueOf(id)).eq("deleted", false));
        return new CommonResponse<>(true);
    }

    @DeleteMapping("/admin/acl/activity/batchRemove")
    @Operation(summary = "批量删除客户活动记录")
    public CommonResponse<Boolean> removeUser(@Parameter(description = "活动名称ID", required = true)@RequestBody ArrayList<String> ids) {
        activityRecordService.updateBatchById(ids.stream().map(Long::valueOf).map(e -> {
            ActivityRecord entity = new ActivityRecord();
            entity.setId(e);
            entity.setDeleted(true);
            entity.setGmtModified(new Date());
            return entity;
        }).toList());
        return new CommonResponse<>(true);
    }
}
